<?php
namespace app\admin\controller;

class User extends Auth{
	public function __construct()
	{
		parent::__construct();
        $this->isLogin();
		$this->User = D('User');
	}
	
	public function index()
	{
        $active['fit']['user']=1;
        $this->assign('active',$active);
        $manage_info = $this->User->manageInfo();
        $this->view->assign('manage_info',$manage_info);

        return $this->view->fetch();
	}
	
	public function add()
	{
		
		//所属用户组
		$belongto = M('authGroup')->select();
		$this->view->assign('belong',$belongto);
		
		if(IS_POST){
		 $passwd = trim(I('post.passwd'));
		 $repasswd = trim(I('post.repasswd'));
			if($passwd === $repasswd){
		     $uid = $this->User->addUser();
		       }
			  if($uid){
				  //同时将uid 和 group_id 传到 auth_group_access中
				  $data['uid']=$uid;
				  $data['group_id']=I('post.group_id');
                  M('authGroupAccess')->add($data);
				  return $this->success('新增成功', U('User/index')); 
			  }else{
				  return $this->error('新增失败', U('User/index')); 
			  }
		}
		
	    return $this->view->fetch();
	}
	
	public function edit($id)
	{
		$edit = $this->User->editInfo($id);
		$this->view->assign('edit',$edit);
		
	    //所属角色
		$belongto = M('authGroup')->select();
		$this->view->assign('belong',$belongto);
		
		$user = M('user')->where('id='.$_GET['id'])->find();
		$this->assign('group_id',$user['group_id']);
		return $this->view->fetch();
		
	}
	
	public function postEdit()
	{
		$lastpasswd = md5(trim($_POST['lastpasswd']));
		$passwd = trim($_POST['passwd']);
		$repasswd = trim($_POST['repasswd']);
		
		$where['id'] = $_POST['id'];
		$correct_pass = $this->User->where($where)->find();
		
		if($passwd === $repasswd && $lastpasswd ===$correct_pass['passwd'] ){
		$post_edit = $this->User->postEdit();
			 if($post_edit !== false){
				 manage_log_success();
				  $data['uid']=I('post.id');
				  $data['group_id']=I('post.group_id');
                  M('authGroupAccess')->where('uid='.$data['uid'])->save($data);
				 return $this->success('修改成功', U('User/index'));
			 }else{
				 manage_log_error();
				 return $this->error('修改失败', U('User/index'));
			 }
		 
		}else{
			manage_log_error();

			 return $this->error('修改失败', U('User/index'));
		}
		
	}
	
	public function del()
	{
		$reconfirm = $this->reconfirm('删除后无法恢复，是否确认删除？');
        if(!$reconfirm){
			 return $this->error('取消删除', U('User/index'));
		}
		$del = $this->User->delUser();
		 if($del){
			 manage_log_success();
			return $this->success('删除成功', U('User/index'));
		 }else{
			 manage_log_error();
			return $this->error('删除失败', U('User/index'));
		 }
	}
	
	public function manage_log()
	{
        $active['efficiency']['log']=1;
        $this->assign('active', $active);
		
		$log = $this->User->mLog();
        $count= count($log);
		$Page = $this->getPage($count,C('PAGE_LOG_NUMS'));
		$manage_log=M('log')->limit($Page->firstRow.','.$Page->listRows)->order('id desc')->select();
		$this->view->assign('manage_log',$manage_log);
		
		$latestDate = $log[0]['time'];
        $this->view->assign('latestDate',$latestDate);
		
		return $this->view->fetch();
	}
	
    public function delManageLog($time)
	{
		$reconfirm = $this->reconfirm('删除后无法恢复，是否确认删除？');
        if(!$reconfirm){
			 return $this->error('取消删除', U('User/manage_log'));
		}
		$del = $this->User->delManageLog($time);
		 if($del){
			 manage_log_success();
			return $this->success('删除成功', U('User/manage_log'));
		 }else{
			 manage_log_error();
			return $this->error('删除失败', U('User/manage_log'));
		 }
	}

	
	/**
	 *  用户组管理
	 */
	
	public function group()
	{
       $active['fit']['group']=1;
	   $this->assign('active',$active);
       $group = M('authGroup')->select();
	   $this->assign('grouplist',$group);
		return $this->view->fetch();
	}
	
	public function group_add()
	{
	   if(IS_POST){
		     $group_add = $this->User->group_add();
			  if($group_add){
				  return $this->success('新增成功', U('User/group')); 
			  }else{
				  return $this->error('新增失败', U('User/group_add')); 
			  }
		}
	    return $this->view->fetch();
		
	}
	
	public function group_edit($id)
	{
		$info = $this->User->group_edit($id);
		$this->assign('info',$info);
		 return $this->fetch();
	}
	
	public function postdata(){
		$info = $this->User->postdata();
		 if($info){
			  return $this->success('修改成功', U('User/group')); 
		  }else{
			  return $this->error('修改失败', U('User/group')); 
		  }
	}
	
	public function group_del($id)
	{
		$info = $this->User->group_del($id);
		 if($info){
			  return $this->success('删除成功', U('User/group')); 
		  }else{
			  return $this->error('删除失败', U('User/group')); 
		  }
	}
	
	
	/**
	 *  用户组成员列表
	 */
	
	public function group_user()
	{
		
		return $this->fetch();
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
